package Task2;

import java.io.*;
import java.util.concurrent.Callable;

public class ThreadRunnable implements Runnable {

    // 重写run方法
    @Override
    public void run() {
            Copyfiles("/Users/minmengjie/Desktop/太阳系","/Users/minmengjie/Desktop/copy");


    }



    public  void Copyfiles(String orgPath, String desPath){
        //1. 新建File类型的对象
        File orgFile = new File(orgPath);
        //2. 判断原始路径下是否存在文件
        if(orgFile.exists()){
            //3. 创建数组遍历目录
            File[] files = orgFile.listFiles();
            //4. 创建存储缓冲区
            byte[] bArr = new byte[1024];
            for(File tf: files){
                String name = tf.getName();
                //5.1 判断需要复制的是文件还是文件夹
                if(tf.isFile()) {
//                    System.out.print("原文件夹中的文件是:");
//                    System.out.println("[" +name +"]");
                    try {
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(tf.getAbsoluteFile()));
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desPath +"" + tf.getName()));
                        int res = 0;
                        while((res = bis.read(bArr)) != -1){
                            bos.write(bArr,0,res);
                        }
                        System.out.println("源文件地址：" + tf.getAbsolutePath() + "----> 复制到新文件地址:" + desPath + "/" + tf.getName());
                        bis.close();
                        bos.close();

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                // 5.2 如果需要复制的是文件夹
                if(tf.isDirectory()){
//                    System.out.print("这个是文件夹:");
//                    System.out.println("{" + name + "}");
                    File copiedFile = new File(desPath + "/" + tf.getName());
                    copiedFile.mkdir();
                    Copyfiles(tf.getAbsolutePath(),desPath + "/" + tf.getName());
                }
            }
        }

    }

}
